<template>
  <div class="dialog">
    <el-dialog :title="titleText" :visible.sync="showDialog" width="45%" top="0" :close-on-click-modal="false" @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称:" prop="materialNo" class="w100p">
              <el-select v-model="formData.materialNo" filterable placeholder="请选择产品名称">
                <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="产品名称:" prop="materialName" class="w100p">
              <el-input v-model="formData.materialName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="产品规格:" prop="materialspec" class="w100p">
              <el-input v-model="formData.materialspec" placeholder="请输入产品规格" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="产品型号:" prop="model" class="w100p">
              <el-input v-model="formData.model" placeholder="请输入产品型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主单位:" prop="unitNo" class="w100p">
              <el-select
                v-model="formData.unitNo"
                clearable
                placeholder="请选择单位"
              >
                <el-option
                  v-for="item,index in unitOptions"
                  :key="index"
                  :label="item.unitName"
                  :value="item.unitNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="原料图片:" prop="pictureNo" class="w100p">
              <el-upload
                ref="upload"
                class="upload-file"
                action
                :limit="1"
                multiple
                :on-remove="params => removePicture(params)"
                :http-request="params => addPicture(params)"
                :file-list="pictureList"
                accept=".png, .jpg, .jpeg, .pdf"
              >
                <el-button
                  v-if="pictureList.length < 1"
                  type="text"
                  class="add-many"
                >
                  <i class="el-icon-plus"></i>
                </el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ''
    },
    show: {
      type: Boolean,
      default: false
    },
    unitOptions: {
      type: Array,
      default: () => []
    },
    options: {
      type: Array,
      default: () => []
    },
    formDataCopy: {
      type: Object,
      default: () => {}
    },
    prodData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      showDialog: this.show,
      // 展示
      formData: {},
      pictureList: [],
      rules: {
        materialNo: [
          {
            required: true,
            message: '请输入产品类型',
            trigger: ['blur', 'change']
          }
        ],
        materialspec: [
          {
            required: true,
            message: '请输入产品规格',
            trigger: ['blur', 'change']
          }
        ],
        materialName: [
          {
            required: true,
            message: '请输入产品名称',
            trigger: ['blur', 'change']
          }
        ],
        unitNo: [
          {
            required: true,
            message: '请选择名称',
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      this.pictureList = [];
    },
    formDataCopy(v) {
      this.formData = v;
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 删除列表图片
    removePicture(file) {
      const index = this.pictureList.findIndex(item => {
        return item.uid === file.uid;
      });
      this.pictureList.splice(index, 1);
    },
    addPicture(file) {
      this.pictureList.push(file.file);
    },
    save(formData) {
      this.$refs[formData].validate(async valid => {
        if (valid) {
          this.$emit('addEvent', this.formData);
          this.close();
        }
      });
    },
    close() {
      this.$emit('closeDialog');
    },
    cancel() {
      this.$emit('closeDialog');
      this.formData = {};
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__wrapper .el-dialog .el-dialog__body .el-form .el-form-item .el-form-item__label {
  min-width: 160px !important;
}
</style>
